<?php

declare(strict_types=1);

namespace app\shop\model\attr;

use app\chat\model\Question;
use app\operations\model\Store;
use app\shop\model\Shop;
use sheep\model\Common;
use think\db\Query;
use think\model\concern\SoftDelete;

class ShopType extends Common
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $name = 'shop_type';

    // 商家服务分类列表
    public function serviceTypeInfo($shopId)
    {
        return $this->where(['parent_id' => 0])
            ->where(function ($query) use ($shopId) {
            if (!empty($shopId)) {
                $query->where('shop_id', $shopId);
            }
        })->select();
    }

    // 子服务列表
    public function list($params)
    {
        $newWhere = [];
        $search   = objectToArray($params['search']);
        if (!empty($params['shop_id'])) {
            $newWhere[] = ['shop_id', '=', $params['shop_id']];
        }
        if (!empty($search) && isset($search['type_name'])) {
            $newWhere[] = ['type_name', $search['type_name'][1], '%' . $search['type_name'][0] . '%'];
        }
        $result = self::where(['status' => 0, 'parent_id' => $params['id']])
            ->with(['service'])
            ->where($newWhere)
            ->order($params['sort'], $params['order'])
            ->withCount('service')
            ->paginate([
                'list_rows' => $params['list_rows'],
                'page' => $params['page'],
            ])->toArray();
        $type   = $this->serviceType($params['shop_id']);
        foreach ($result['data'] as $k => $item) {
            $result['data'][$k]['service_type_name'] = $type[$item['parent_id']] ?? '';
            $result['data'][$k]['service_number']    = $item['service_count'];
        }
        return $result;
    }

    // 属性添加
    public function dataCreate($params)
    {
        $this->allowField(['type_name', 'shop_id', 'parent_id', 'icon_url', 'remake'])->save($params);
        return $this->id;
    }

    // 更新属性方法
    public function editCreate($params): bool
    {
        $brandModel = self::find($params['id']);
        return $brandModel->allowField(['type_name', 'shop_id', 'parent_id', 'icon_url', 'remake'])->save($params);
    }

    // 商家服务子服务详情方法
    public function subServiceInfo($params)
    {
        return $this->alias('s')->where(['id' => $params])->with(['service'])->find();
    }

    // 属性删除方法
    public function delData($params): bool
    {
        return self::destroy($params);
    }

    // 服务项目模型关联
    public function service()
    {
        return $this->hasMany(ShopTypeServiceProject::class, 'shop_type_id', 'id');
    }

    // 服务模型关联
    public function serve()
    {
        return $this->hasMany(ShopType::class, 'parent_id', 'id');
    }

    public function store()
    {
        return $this->hasOne(Store::class, 'id', 'shop_id');
    }

    public function serviceType($shopId)
    {
        return $this->where(['parent_id' => 0])
            ->where(function ($query) use ($shopId) {
                if (!empty($shopId)) {
                    $query->where('shop_id', $shopId);
                }
            })
            ->column('type_name', 'id');
    }
}
